package com.zdsoft.blp.zongbuju.shouye.linliquan.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.isLoginSuccess.IsLoginSuccess;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.ZZLYadapter;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/31.
 */
public class LLLvYouActivity extends BaseActivity implements NetCallback,XListView.IXListViewListener{
    private XListView xlv;
    private List<LinLi> list;
    private List<LinLi> mlist;
    private ImageView title_bianji;
    private TextView tv_title;
    private LinLi linLi;
    private int pageNo=1,source;
    private ZZLYadapter zzlYadapter;
    private Intent intent;

    @Override
    protected void init() {
        setContentView(R.layout.linli_zizhuluyou);
        xlv = find(R.id.linli_zizhulvyou_lv);
        //设置标题
        tv_title = find(R.id.tv_title);
        mlist=new ArrayList<>();
        intent = getIntent();
        String title = intent.getStringExtra("title");
        tv_title.setText(title);
        xlv.setXListViewListener(this);
        xlv.setPullRefreshEnable(true);//设置可以刷新
        xlv.setPullLoadEnable(true);//设置可以加载更多
        //跳转编辑页面
        title_bianji = find(R.id.title_bianji);
        title_bianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (IsLoginSuccess.isloginSuccess(LLLvYouActivity.this,true)){
                    intent = new Intent(LLLvYouActivity.this,FBTYActivity.class);
                    intent.putExtra("title","发消息");
                    intent.putExtra("aSource","自助旅游");
                    startActivity(intent);
                }

            }
        });
        showMoreData();
        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent=new Intent(LLLvYouActivity.this,XQLYActivity.class);
                intent.putExtra("id",mlist.get(position-1).getId());
                startActivity(intent);
            }
        });
    }

    private void showMoreData() {

        intent=getIntent();
        source=intent.getIntExtra("source",1);
        Net net=new Net();
        net.url= API.LYINFO_URL;
        net.nameSpace=API.LYINFO_NAMESPACE;
        Map<String,Object> map=new HashMap<>();
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String userId = sp.getString("userId","");
        String communityCode = sp.getString("communityCode","00001");
        if (source==2){
            net.methodName=API.MYLYINFOLIST ;
            map.put("pageNo",pageNo);
            map.put("pageSize",10);
            map.put("communityCode",communityCode);
            map.put("condition","");
            map.put("curTime","");
            map.put("userId",userId);
            net.parms=map;
            net.code=2;

        }else {
            net.methodName=API.LYINFOLIST ;
            map.put("pageNo",pageNo);
            map.put("pageSize",10);
            map.put("communityCode",communityCode);
            map.put("condition","");
            map.put("curTime","");
            net.parms=map;
            net.code=1;
        }
        getServer(net,this);
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        Log.e("res",res);
       if (flag){
           if (pageNo==1){
               mlist=jiexi(res);
               zzlYadapter = new ZZLYadapter(mlist, LLLvYouActivity.this);
               xlv.setAdapter(zzlYadapter);
               xlv.stopRefresh();
           }else {
               list=jiexi(res);
               for (LinLi linLis:list){
                   mlist.add(linLis);
               }
               zzlYadapter.setListData(mlist);
               xlv.stopLoadMore();
           }
       }


    }

    private List<LinLi> jiexi(String res) {
        list=new ArrayList<>();
        try {
            JSONObject object=new JSONObject(res);
            String total=object.getString("total");
            String pageNo=object.getString("pageNo");
            String pageSize=object.getString("pageSize");
            JSONArray array=object.getJSONArray("rows");
            for(int i=0;i<array.length();i++){
                JSONObject object2=array.getJSONObject(i);
                String id=object2.getString("id");
                String picPath=object2.getString("picPath");
                String title=object2.getString("title");
                String contactPhone=object2.getString("contactPhone");
                String contact=object2.getString("contact");
                int approvedResult=object2.getInt("approvedResult");
               if (source==2){
                   linLi=new LinLi(id,title,contact,contactPhone,picPath,approvedResult);
                   linLi.setCode(2);
               }else {
                   linLi=new LinLi(id,title,contact,contactPhone,picPath);
               }

                list.add(linLi);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void onRefresh() {
        mlist=new ArrayList<>();
        pageNo=1;
        showMoreData();
    }

    @Override
    public void onLoadMore() {
        pageNo++;
        showMoreData();

    }
}
